﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.ComponentModel;

namespace produccion.clases
{
    public class CLSAUDENTP : ObservableObject
    {
        MDOgeneral mod = new MDOgeneral();

        #region Metodos
        // Permite consultar por tipo de entrada y numero de hoja
        public List<CLSAUDENTP> listaHoja()
        {
            try
            {
                string query = "select TIPENT_AEN, HOJNRO_AEN, CONSEC_AEN, CODMAT_AEN, CODCOL_AEN, CODPRV_AEN, NROPED_AEN, CANENT_AEN, ANTES_AEN, DESPUES_AEN from AUDENTP where TIPENT_AEN = '" + TIPENT_AEN + " and HOJNRO_AEN ='" + HOJNRO_AEN + "'";
                DataTable dtt = mod.consulta(query);
                List<CLSAUDENTP> listaAudi = new List<CLSAUDENTP>();
                foreach (DataRow dr in dtt.Rows)
                {
                    listaAudi.Add(new CLSAUDENTP 
                    { TIPENT_AEN = dr["TIPENT_AEN"].ToString(),
                      HOJNRO_AEN = dr["HOJNRO_AEN"].ToString(),
                      CONSEC_AEN = Convert.ToInt32(dr["CONSEC_AEN"]),
                      CODMAT_AEN = dr["CODMAT_AEN"].ToString(),
                      CODCOL_AEN = dr["CODCOL_AEN"].ToString(),
                      CODPRV_AEN = dr["CODPRV_AEN"].ToString(),
                      NROPED_AEN = Convert.ToInt32(dr["NROPED_AEN"]),
                      CANENT_AEN = Convert.ToDouble(dr["CANENT_AEN"]),
                      ANTES_AEN = Convert.ToDouble(dr["ANTES_AEN"]),
                      DESPUES_AEN = Convert.ToDouble(dr["DESPUES_AEN"]) 
                    });
                }
                return listaAudi;
            }
            catch (Exception ex) { throw ex; }
        }

        // Permite consultar por material y color
        public List<CLSAUDENTP> listaMaterial()
        {
            try
            {
                string query = "select TIPENT_AEN, HOJNRO_AEN, CONSEC_AEN, CODMAT_AEN, CODCOL_AEN, CODPRV_AEN, NROPED_AEN, CANENT_AEN, ANTES_AEN, DESPUES_AEN from AUDENTP where CODMAT_AEN = '" + CODMAT_AEN + " and CODCOL_AEN ='" + CODCOL_AEN + "'";
                DataTable dtt = mod.consulta(query);
                List<CLSAUDENTP> listaAudi = new List<CLSAUDENTP>();
                foreach (DataRow dr in dtt.Rows)
                {
                    listaAudi.Add(new CLSAUDENTP
                    {
                        TIPENT_AEN = dr["TIPENT_AEN"].ToString(),
                        HOJNRO_AEN = dr["HOJNRO_AEN"].ToString(),
                        CONSEC_AEN = Convert.ToInt32(dr["CONSEC_AEN"]),
                        CODMAT_AEN = dr["CODMAT_AEN"].ToString(),
                        CODCOL_AEN = dr["CODCOL_AEN"].ToString(),
                        CODPRV_AEN = dr["CODPRV_AEN"].ToString(),
                        NROPED_AEN = Convert.ToInt32(dr["NROPED_AEN"]),
                        CANENT_AEN = Convert.ToDouble(dr["CANENT_AEN"]),
                        ANTES_AEN = Convert.ToDouble(dr["ANTES_AEN"]),
                        DESPUES_AEN = Convert.ToDouble(dr["DESPUES_AEN"])
                    });
                }
                return listaAudi;
            }
            catch (Exception ex) { throw ex; }
        }

        // Consultar por tipo de entrada y numero de hoja y llena toda las propiedades con los valores de la consulta
        public void consultar()
        {
            try
            {
                string query = "select TIPENT_AEN, HOJNRO_AEN, CONSEC_AEN, CODMAT_AEN, CODCOL_AEN, CODPRV_AEN, NROPED_AEN, CANENT_AEN, ANTES_AEN, DESPUES_AEN from AUDENTP where TIPENT_AEN = '" + TIPENT_AEN + " and HOJNRO_AEN ='" + HOJNRO_AEN + "'";
                DataTable dtt = mod.consulta(query);
                if (dtt.Rows.Count > 0)
                {
                    this.TIPENT_AEN = dtt.Rows[0]["TIPENT_AEN"].ToString();
                    this.HOJNRO_AEN = dtt.Rows[0]["HOJNRO_AEN"].ToString();
                    this.CONSEC_AEN = Convert.ToInt32(dtt.Rows[0]["CONSEC_AEN"]);
                    this.CODMAT_AEN = dtt.Rows[0]["CODMAT_AEN"].ToString();
                    this.CODCOL_AEN = dtt.Rows[0]["CODCOL_AEN"].ToString();
                    this.CODPRV_AEN = dtt.Rows[0]["CODPRV_AEN"].ToString();
                    this.NROPED_AEN = Convert.ToInt32(dtt.Rows[0]["NROPED_AEN"]);
                    this.CANENT_AEN = Convert.ToDouble(dtt.Rows[0]["CANENT_AEN"]);
                    this.ANTES_AEN = Convert.ToDouble(dtt.Rows[0]["ANTES_AEN"]);
                    this.DESPUES_AEN = Convert.ToDouble(dtt.Rows[0]["DESPUES_AEN"]);

                    EXISTE = true;
                }
                else
                    EXISTE = false;
            }
            catch (Exception ex) { throw ex; }
        }

        public DataTable conEntradas(String StrCodigoProveedor, String StrNumeroPedido)
        {
            try
            {
                //string query = "select a.HOJNRO_AEN, a.CODMAT_AEN, a.CODCOL_AEN, sum(b.CANPRO_DEN ) as CANENT_AEN, ''||to_char(c.FECENT_EEN,'yyyy/mm/dd')||'' AS FECENT_EEN from AUDENTP a inner join detentp b on a.TIPENT_AEN=b.TIPENT_DEN and a.HOJNRO_AEN=b.HOJNRO_DEN and a.CODMAT_AEN=b.CODMAT_DEN and a.CODCOL_AEN=b.CODCOL_DEN inner join encentp c on b.hojnro_den=c.hojnro_een and b.tipent_den=c.tipent_een  where a.CODMAT_AEN = '" + CODMAT_AEN + "' and a.CODCOL_AEN ='" + CODCOL_AEN + "' group by  a.HOJNRO_AEN, a.CODMAT_AEN, a.CODCOL_AEN, c.FECENT_EEN order by c.FECENT_EEN desc ";
                string query = "select a.HOJNRO_AEN, a.CODMAT_AEN, a.CODCOL_AEN, sum(b.CANPRO_DEN ) as CANENT_AEN, c.DOCREF_EEN,  ";
                query = query + "''||to_char(c.FECENT_EEN,'DD/MM/YYYY')||'' AS FECENT_EEN  ";
                query = query + "from AUDENTP a  ";
                query = query + "inner join detentp b on a.TIPENT_AEN=b.TIPENT_DEN and  ";
                query = query + "a.HOJNRO_AEN=b.HOJNRO_DEN and  ";
                query = query + "a.CODMAT_AEN=b.CODMAT_DEN and  ";
                query = query + "a.CODCOL_AEN=b.CODCOL_DEN  ";
                query = query + "Inner join encentp c on a.TIPENT_AEN=c.TIPENT_EEN and  ";
                query = query + "a.HOJNRO_AEN=c.HOJNRO_EEN ";
                query = query + "where a.CODMAT_AEN = '" + CODMAT_AEN + "' and a.CODCOL_AEN ='" + CODCOL_AEN + "' and a.codprv_aen = '"+ StrCodigoProveedor +"' and a.nroped_aen = "+ StrNumeroPedido +" ";
                query = query + "group by  a.HOJNRO_AEN, a.CODMAT_AEN, a.CODCOL_AEN, c.docref_een, c.FECENT_EEN order by c.FECENT_EEN desc ";
                return mod.consulta(query);
            }
            catch (Exception ex) { throw ex; }
        }

        #endregion

        #region Propiedades

        private string _TIPENT_AEN;
        public string TIPENT_AEN
        {
            get { return _TIPENT_AEN; }
            set { _TIPENT_AEN = value; RaisePropertyChangedEvent("TIPENT_AEN"); }
        }

        private string _HOJNRO_AEN;
        public string HOJNRO_AEN
        {
            get { return _HOJNRO_AEN; }
            set { _HOJNRO_AEN = value; RaisePropertyChangedEvent("HOJNRO_AEN"); }
        }

        private Int32 _CONSEC_AEN;
        public Int32 CONSEC_AEN
        {
            get { return _CONSEC_AEN; }
            set { _CONSEC_AEN = value; RaisePropertyChangedEvent("CONSEC_AEN"); }
        }

        private string _CODMAT_AEN;
        public string CODMAT_AEN
        {
            get { return _CODMAT_AEN; }
            set { _CODMAT_AEN = value; RaisePropertyChangedEvent("CODMAT_AEN"); }
        }

        private string _CODCOL_AEN;
        public string CODCOL_AEN
        {
            get { return _CODCOL_AEN; }
            set { _CODCOL_AEN = value; RaisePropertyChangedEvent("CODCOL_AEN"); }
        }

        private string _CODPRV_AEN;
        public string CODPRV_AEN
        {
            get { return _CODPRV_AEN; }
            set { _CODPRV_AEN = value; RaisePropertyChangedEvent("CODPRV_AEN"); }
        }

        private Int32 _NROPED_AEN;
        public Int32 NROPED_AEN
        {
            get { return _NROPED_AEN; }
            set { _NROPED_AEN = value; RaisePropertyChangedEvent("NROPED_AEN"); }
        }

        private double _CANENT_AEN;
        public double CANENT_AEN
        {
            get { return _CANENT_AEN; }
            set { _CANENT_AEN = value; RaisePropertyChangedEvent("CANENT_AEN"); }
        }

        private double _ANTES_AEN;
        public double ANTES_AEN
        {
            get { return _ANTES_AEN; }
            set { _ANTES_AEN = value; RaisePropertyChangedEvent("ANTES_AEN"); }
        }

        private double _DESPUES_AEN;
        public double DESPUES_AEN
        {
            get { return _DESPUES_AEN; }
            set { _DESPUES_AEN = value; RaisePropertyChangedEvent("DESPUES_AEN"); }
        }

        private Boolean _EXISTE = false;
        public Boolean EXISTE
        {
            get { return _EXISTE; }
            set { _EXISTE = value; RaisePropertyChangedEvent("EXISTE"); }
        }

        #endregion
    }
}
